package com.sinoeyes.compent.service.impl;

import com.sinoeyes.common.constant.Constants;
import com.sinoeyes.common.utils.DateUtils;
import com.sinoeyes.common.utils.SnakeFlowUtil;
import com.sinoeyes.common.utils.StringUtils;
import com.sinoeyes.compent.mapper.DimensionInfoMapper;
import com.sinoeyes.compent.po.DimensionInfo;
import com.sinoeyes.compent.service.IDimensionInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 维度信息Service业务层处理
 *
 * @author aries
 * @date 2019-09-09
 */
@Service
@Slf4j
public class DimensionInfoServiceImpl implements IDimensionInfoService {
    @Autowired
    private DimensionInfoMapper dimensionInfoMapper;


    /**
     * 查询维度信息
     *
     * @param id 维度信息ID
     * @return 维度信息
     */
    @Override
    public DimensionInfo selectDimensionInfoById(String id) {
        return dimensionInfoMapper.selectDimensionInfoById(id);
    }

    /**
     * 查询维度信息列表
     *
     * @param dimensionInfo 维度信息
     * @return 维度信息
     */
    @Override
    public List<DimensionInfo> selectDimensionInfoList(DimensionInfo dimensionInfo) {
        return dimensionInfoMapper.selectDimensionInfoList(dimensionInfo);
    }

    /**
     * 新增维度信息
     *
     * @param dimensionInfo 维度信息
     * @return 结果
     */
    @Override
    public int insertDimensionInfo(DimensionInfo dimensionInfo) {
        dimensionInfo.setId(SnakeFlowUtil.getUUID());
        dimensionInfo.setActive(1);
        dimensionInfo.setCreateTime(DateUtils.getNowDate());
        dimensionInfo.setUpdateTime(DateUtils.getNowDate());
        //SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        dimensionInfo.setOperatorId("少男");
        return dimensionInfoMapper.insertDimensionInfo(dimensionInfo);
    }

    /**
     * 修改维度信息
     *
     * @param dimensionInfo 维度信息
     * @return 结果
     */
    @Override
    public int updateDimensionInfo(DimensionInfo dimensionInfo) {
        //SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        dimensionInfo.setOperatorId("少男");
        dimensionInfo.setUpdateTime(DateUtils.getNowDate());
        return dimensionInfoMapper.updateDimensionInfo(dimensionInfo);
    }

    /**
     * 删除维度信息信息
     *
     * @param id 维度信息ID
     * @return 结果
     */
    @Override
    public int deleteDimensionInfoById(String id, String strFlag) {
        String[] ids = id.split(",");
        if(ids.length == 0){return 0;}
        //SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        DimensionInfo dimensionInfo = new DimensionInfo();
        dimensionInfo.setId(id);
        dimensionInfo.setUpdateTime(DateUtils.getNowDate());
        dimensionInfo.setOperatorId("少男");
        if (!StringUtils.isBlank(strFlag)) {
            if(Constants.ENABlE_FLAG.equals(strFlag)){
                if(ids.length > 1){
                    return dimensionInfoMapper.enableDimensionInfoByIds(dimensionInfo);
                }
                return dimensionInfoMapper.enableDimensionInfoById(dimensionInfo);
            }
            if(Constants.DEL_FLAG.equals(strFlag)){
                if(ids.length > 1){
                    return dimensionInfoMapper.deleteDimensionInfoByIds(dimensionInfo);
                }
                return dimensionInfoMapper.deleteDimensionInfoById(dimensionInfo);
            }
        }
        return 0;
    }
}
